import React, { Component } from 'react';
import { Button } from 'antd';
import { connect } from 'dva';

@connect(({ menu }) => ({ menuAuthority: menu.menuAuthority }))
export default class AdButton extends Component {
    state = {
        isAuthority: false,
    };

    componentDidMount() {
        const { menuAuthority, dispatch } = this.props;
        this.getCode(this.props);
    }

    componentWillReceiveProps(nextProps) {
        const { menuAuthority } = nextProps;
        if (JSON.stringify(menuAuthority) !== JSON.stringify(this.props.menuAuthority)) {
            this.getCode(nextProps);
        }
    }

    // getCode = props => {
    //   const { code, menuAuthority } = props;
    //   const { isAuthority } = this.state;
    //   if (!code) {
    //     this.setState({ isAuthority: true });
    //     return;
    //   }
    //   let operateData = [];
    //   let newOperateData = [];
    //   menuAuthority.map(item => item.menuBODetail.map(item => operateData.push(item)));
    //   operateData.map(item => item.menuBODetail.map(item => newOperateData.push(item)));
    //   const newAuthority = newOperateData.filter(item => item.code === code);
    //   if (newAuthority.length > 0 && !isAuthority) {
    //     this.setState({ isAuthority: true });
    //   }
    // };

    /*
    权限分为三类:
  1.客户;
  2.CM;
  3.管理员;
   
  1.客户是全部都能看和下载，但是不能编辑和添加,删除;
  2.CM就是只有下单和跟踪的权限,报表和账单他们是看都不能看的;
  3.管理员账户拥有所有功能;
  返回：/api/User/User/GetUserInfo
  UserInfo {
  type (string, optional): 用户类型( C 委托单位 S 发货人 G 收货人 ) ,
  }
    C 委托单位（），S发货人（CM---客户)，管理员 A
  */
    getCode = props => {
        // const { code } = props;
        // const user = localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user')) : {};
        // const { type } = user;
        // if (!code || type == 'A' || type == 'C') {
        //     this.setState({ isAuthority: true });
        //     return;
        // }
        // if (code == 'operate' && type == 'S') {
        //     this.setState({ isAuthority: false });
        //     return;
        // }
        this.setState({ isAuthority: true });

    };

    render() {
        const { code, text, menuAuthority, mode, dispatch, ...rest } = this.props;
        const { isAuthority } = this.state;
        return mode === 'a' ? (
            isAuthority ? (
                <a {...rest}>{text}</a>
            ) : (
                    <span {...rest}>{text}</span>
                )
        ) : (
                isAuthority && <Button {...rest}>{text}</Button>
            );
    }
}
